﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using log4net;
using DevExpress.XtraEditors;

namespace MMS
{
    public static class Utils
    {
        public static string APP_TITLE = "Quản lý cho thuê vật tư";

        public static UserDTO UserLoggedIn;
        public static string Repository;
        private static readonly ILog log =
            LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static void SetConfigParam(Param param, string value)
        {
            Configuration config =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            try
            {
                if (param != Param.ConnectionString)
                {
                    config.AppSettings.Settings.Remove(param.Value);
                    config.AppSettings.Settings.Add(param.Value, value);
                    //save config file
                    config.Save(ConfigurationSaveMode.Full);
                }
                else
                {
                    ConfigurationManager.ConnectionStrings.Remove(param.Value);
                    ConfigurationManager.ConnectionStrings
                        .Add(new ConnectionStringSettings(param.Value, value));
                    //EncryptSection(config, "connectionStrings", true);
                }
            }
            catch (Exception e)
            {
                log.Error(e);
            }
            //Force reload
            ConfigurationManager.RefreshSection("appSettings");
            ConfigurationManager.RefreshSection("connectionStrings");
        }

        public static string GetConfigParam(Param param)
        {
            Configuration config =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.GetSection("a");
            //EncryptSection(config, "connectionStrings", true);
            if (config.AppSettings.Settings[param.Value] != null)
            {
                return config.AppSettings.Settings[param.Value].Value;
            }
            else if (ConfigurationManager.ConnectionStrings[param.Value] != null)
            {
                return ConfigurationManager.ConnectionStrings[param.Value].ConnectionString;
            }
            else
            {
                SetConfigParam(param, "");
                log.Error(String.Format("param: {0} is not found in config file", param));
                return "";
            }
        }

        public static void EncryptSection(Configuration config, string sectionString, bool b)
        {
            ConfigurationSection section = config.GetSection(sectionString);
            if (!(section.SectionInformation.IsProtected)
            && !section.SectionInformation.IsLocked
            && !section.IsReadOnly())
            {
                section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full);
            }
            ConfigurationManager.RefreshSection(sectionString);
        }

        public static void ShowNotication(string message)
        {
            System.Windows.Forms.MessageBox.Show(message, "Thông báo", MessageBoxButtons.OK,
                                                       MessageBoxIcon.Information);
        }

        public static void ShowError(string message)
        {
            System.Windows.Forms.MessageBox.Show(message, "Thông báo lỗi", MessageBoxButtons.OK,
                                                       MessageBoxIcon.Error);
        }
        public static DialogResult ShowAuthentication(string message)
        {
            return System.Windows.Forms.MessageBox.Show(message, "Xác nhận", MessageBoxButtons.YesNo,
                                                       MessageBoxIcon.Question);
        }

        public static byte[] EncryptData(string data)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher
                = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes;
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
            return hashedBytes;
        }
        public static string Md5(string input)
        {
            return BitConverter.ToString(EncryptData(input)).Replace("-", "").ToLower();
        }

        public static void startTimer(Timer timer)
        {
            timer.Interval = (4000);             // Timer will tick evert 10 seconds
            timer.Enabled = true;                       // Enable the timer
            timer.Start();

        }
        public static void showDialogCannotDelete()
        {
            MessageBox.Show(MessageBundle.CANNOT_DELETE_CONTENT.Value, MessageBundle.DELETE_WARNING_TITLE.Value,
                                   MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
        }
        private static void setFocusOnControl(object control)
        {
            if (control.GetType() == typeof(TextEdit))
            {

            }
        }
    }
}
